package com.pignest.hotel.ui.selfhelp;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.pignest.framework.util.Utils;
import com.pignest.hotel.beans.Card;
import com.pignest.hotel.beans.Living;
import com.pignest.hotel.beans.LivingCustom;
import com.pignest.hotel.beans.LivingSpecifics;
import com.pignest.hotel.beans.Member;
import com.pignest.hotel.beans.Room;
import com.pignest.hotel.definition.LivingState;
import com.pignest.hotel.definition.LivingType;
import com.pignest.hotel.definition.OpenRoomType;
import com.pignest.hotel.definition.RoomLiveStatus;
import com.pignest.hotel.service.MemberService;
import com.pignest.hotel.service.RoomService;
import com.pignest.hotel.service.UserService;
import com.pignest.hotel.spring.Services;
import com.pignest.hotel.system.Config;
import com.pignest.hotel.system.Global;
import com.pignest.hotel.system.Hotel;
import com.pignest.hotel.ui.room.RoomHistogram;
import com.pignest.hotel.ui.room.RoomView;
import com.pignest.hotel.util.DateUtil;
import com.pignest.hotel.util.MoneyUtil;
import com.pignest.hotel.util.UIUtils;

public class SelfHelpRoomHistogram extends RoomHistogram {

	private static final long serialVersionUID = -8676818187519139517L;
	private String roomTypeId;
	private String card;
	private Member curMember;

	@Override
	protected List<Room> getRooms() {
		setLineCapacity(Config.getLineCapacityOfSelfService());
		setShowFlags(RoomView.SHOW_USE_STATUS | RoomView.SHOW_FEATURE);
		RoomService service = Services.getRoomService();
		return service.listSelfHelpRooms(roomTypeId);
	}
	
	public void setRoomTypeId(String roomTypeId) {
		this.roomTypeId = roomTypeId;
		refresh();
	}
	
	public void onLiving() {
		if (StringUtils.isEmpty(card)) {
			UIUtils.alert("请先登录!");
			return;
		}
		String roomId = getSelectedRoom();
		if (roomId == null) {
			UIUtils.alert("请选择一个房间");
			return;
		}
		//TODO 根据card和roomId入住确认, 并选择时间后入住后入住
	}
	public Member queryMem()
	{
		MemberService service =Services.getMemberService();
		UserService uServices =Services.getUserService();
		String cardNumber =uServices.getCardNumberBySNR(card);
		return service.queryMemberOnCard(cardNumber);
	}
	public void setCard(String card) {
		this.card = card;
	}
	public String getCard() {
		return card;
	}
	
	public Member getCurMember() {
		return curMember;
	}

	public void setCurMember(Member curMember) {
		this.curMember = curMember;
	}


    public void initSelect()
    {
    	
    }
	public String selfLiving(String selectRoomId,Member m,Date d1,String time,Card card,OpenRoomType oType,int adjustPrice)
	{
		try{
			d1 = DateUtil.parseDate(d1, time);
		   if (d1.getTime() < System.currentTimeMillis() + DateUtil.ONE_HOUR) {
			 return "预离时间填写不正确";
		   }
		   MemberService service =Services.getMemberService();
		   RoomService  roomService =Services.getRoomService();
		   Room selectRoom =roomService.getRoom(selectRoomId);
		   int overDaft =m.getMemberLevel().getOverdraft();
		  // int mP=service.getMemberPrice(m.getId(), selectRoom.getType().getId());
		   int balance=m.getBalance();
			if(m.getMemberLevel().getSelfhelplive().equals("否"))
			{
				return "很抱歉当前会员不能自助入住！你的会员为"+m.getMemberLevel().getMemberlevel()+"请联系前台";
			}
			if(balance+overDaft<adjustPrice){
				return "当前余额不足，余额:"+Utils.toPrice(balance)+"可透支:"+Utils.toPrice(overDaft)+"请充值！";
			}
			String sOpenType =oType.name();
			Living l = new Living();
			l.setLiveType(LivingType.SELF_LIVING.name());
			l.setRoom(selectRoom);
			l.setMember(m);
			l.setLiveTime(new Date());
			l.setLiveState(LivingState.LIVING.name());
			l.setCustormName(m.getName());
			l.setSex(m.getSex());
			l.setPhone(m.getMobile());
			l.setPassportType(m.getCertificatType());
			l.setPassportNumber(m.getCertificateNo());
			l.setRoomPrice(selectRoom.getType().getPrice());
			l.setOpenRoomType(sOpenType);
			l.setAdjustPrice(adjustPrice);
			l.setSelfHelp(true);
			l.setStorage(0);//会员不设置押金
			m.setBalance(0);//
			l.setAvailable(l.getStorage());
			l.setUsed(0);
			l.setCardNumber(this.card);

			l.setGrantDate(d1);
			l.setExpectLeaveDate(d1);
			
			if (l.getSerialNumber() == null)
				l.setSerialNumber(Global.getLivineSerialNumber());
			LivingSpecifics spec = l.renderSpecifics();
			spec.setPaymentMethod("会员卡");
			
			/**初始设置账*/

				/*TbAccounts accounts=new TbAccounts();
				accounts.setLiving(l);
				//accounts.setType("押金");
				accounts.setType(InComeItemType.YJ.name());
				accounts.setPrice(Hotel.self_living_preMoney);
				accounts.setCounts(1);
				accounts.setEndstate("否");
				accounts.setName(InComeItemType.YJ.getValue());
				accounts.setOptime(new Date());
				accounts.setPrice2(Hotel.self_living_preMoney);*/
			/***设置主要客人**/
			LivingCustom lc = new LivingCustom();
			lc.setLiving(l);
			lc.setName(m.getName());
			lc.setIshost("是");
			lc.setCertificatetype(m.getCertificatType());
			lc.setCertificateno(m.getCertificateNo());
			lc.setPhone(m.getMobile());
			lc.setSex(m.getSex());

			StringBuffer msg = new StringBuffer();
			String respectfully="";//尊称
			if(m.getSex().equals("男"))
				respectfully="先生";
			else
				respectfully="小姐/女士";
			msg.append("欢迎你：").append(m.getName()+respectfully).append("\n");
			msg.append("当前房间为:").append(selectRoom.getName()).append("\n");
			msg.append("开房方式：").append(oType.getDisplayName()).append("\n");
			msg.append("当前房间价:").append(Utils.toPrice(selectRoom.getType().getPrice())).append("\n");
			
			msg.append("房价为:").append(MoneyUtil.toString(adjustPrice)).append("\n");
			
			msg.append("当前会员余额：").append(MoneyUtil.toString(balance)).append("\n");
			msg.append("可透支金额:").append(Utils.toPrice(overDaft)).append("\n");
			msg.append("当前积分为：").append(m.getIntegral()).append("\n");
			//msg.append("本次将从你的余额中预扣:").append(MoneyUtil.toString(Hotel.self_living_preMoney)).append("\n");
			msg.append("请不要将会员卡移开！谢谢!");
			if(UIUtils.confirm(msg.toString())){
				selectRoom.setLiveStatus(RoomLiveStatus.FILLED.name());
				Services.getRoomService().updateRoom(selectRoom);
                Services.getReceptionService().updateSelfLiving(l, spec, lc, selectRoom, null,card);
				return null;
			}else
				return "取消";
		}catch(Exception e){
			e.printStackTrace();
			return e.getMessage();
		}
	}
}
